package com.github.edu.web.user.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.DigestUtil;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.page.entity.Pager;
import com.github.admin.edu.oauth.server.api.entity.TSysOrganization;
import com.github.admin.edu.oauth.server.api.entity.TSysUser;
import com.github.edu.client.common.service.impl.BaseAbstractService;
import com.github.edu.web.organization.domain.OrganizationManagerDomain;
import com.github.edu.web.user.domain.UserManagerDomain;
import com.github.edu.web.user.service.UserManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-1
 */
@Slf4j
@Service
public class UserManagerServiceImpl extends BaseAbstractService implements UserManagerService {

    @Autowired
    private UserManagerDomain domain;

    @Override
    public JsonArray<TSysUser> getAllByPage(TSysUser tSysOauth, Pager pager) {
        String code = jsonEntity(tSysOauth, pager);
        JsonArray<TSysUser> jsonArray = null;
        if (!StringUtils.isEmpty(code)) {
            String data = domain.getALLByPager(code);
            if (!StringUtils.isEmpty(data)) {
                jsonArray = JsonUtils.toCollection(data, new TypeReference<JsonArray<TSysUser>>() {
                });
            }
        }
        return jsonArray;
    }

    @Override
    public TSysUser getEntity(String aLong) {
        JsonEntity<TSysUser> jsonEntity = JsonUtils.toCollection(domain.getEntity(aLong), new TypeReference<JsonEntity<TSysUser>>() {
        });
        return jsonEntity.getData();
    }

    @Override
    public TSysUser saveOrUpdate(TSysUser tSysOauth) {
        JsonEntity<TSysUser> jsonEntity = new JsonEntity<>();
        jsonEntity.setData(tSysOauth);
        if(StringUtils.isEmpty(tSysOauth.getPassword())){
            tSysOauth.setPassword(DigestUtil.md5("000000"));
        }
        String code = domain.saveOrUpdate(JsonUtils.toJson(jsonEntity));
        JsonEntity<TSysUser> json = JsonUtils.toCollection(code, new TypeReference<JsonEntity<TSysUser>>() {
        });
        if (null != json) {
            return json.getData();
        }
        return null;
    }

    @Override
    public String delete(String ids) {
        return domain.deleteAll(ids);
    }

    @Autowired
    private OrganizationManagerDomain managerDomain;

    @Override
    public List<TSysOrganization> getAllOrganization() {
        String code=managerDomain.getAllEntity("","","");
        if(!StringUtils.isEmpty(code)){
            JsonArray<TSysOrganization> jsonArray=JsonUtils.toCollection(code, new TypeReference<JsonArray<TSysOrganization>>() {
            });
            if(null!=jsonArray){
                return jsonArray.getDataList();
            }
        }
        return null;
    }
}
